﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using BrewXML.Core.Data;
using BrewXML.Core.Services.NavigationServices;
using BrewXML.Core.ViewModels;
using BrewXML.WindowsPhone.Data;
using Cirrious.CrossCore;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace BrewXML.WindowsPhone
{
    public partial class HopDetailPage : PhoneApplicationPage
    {

        #region View Model
        public HopDetailPageViewModel ViewModel
        {
            get
            {
                return this.DataContext as HopDetailPageViewModel;
            }
        }
        #endregion

        #region OnNavigateTo

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

            base.OnNavigatedTo(e);
            if (ViewModel.EnableAppbar == true)
            {
                this.ApplicationBar.IsVisible = true;
            }

            else
            {
                this.ApplicationBar.IsVisible = false;
            }

        }
        #endregion
        
        #region Build Application Bar
        private void BuildApplicationBar()
        {
            var appBar = new ApplicationBar() { Opacity = 1, Mode = ApplicationBarMode.Default };
            var btnSave = new ApplicationBarIconButton(new Uri("/Asset/save.png", UriKind.Relative)) { Text = "Save" };
            btnSave.Click += (sender, args) =>
            {
                if (!ViewModel.AllowAdd)
                {
                    Mvx.Resolve<HomePageViewModel>().HOPS.Save("Hops.xml", "HOPS");
                    var ttt = Mvx.Resolve<HomePageViewModel>().HOPS;
                    Mvx.Resolve<HomePageViewModel>().HOPS = null;
                    Mvx.Resolve<HomePageViewModel>().HOPS = ttt;
                }
                else
                {
                    Mvx.Resolve<HomePageViewModel>().HOPS.Add(ViewModel.Hop);
                    Mvx.Resolve<HomePageViewModel>().HOPS.Save("Hops.xml", "HOPS");
                    var ttt = Mvx.Resolve<HomePageViewModel>().HOPS;
                    Mvx.Resolve<HomePageViewModel>().HOPS = null;
                    Mvx.Resolve<HomePageViewModel>().HOPS = ttt;
                    ViewModel.AllowAdd = false;
                }
                
            };
            appBar.Buttons.Add(btnSave);
            var btndelete = new ApplicationBarIconButton(new Uri("/Asset/delete.png", UriKind.Relative)) { Text = "Delete" };
            btndelete.Click += (sender, args) =>
            {
                int index = Mvx.Resolve<HomePageViewModel>().HOPS.IndexOf(ViewModel.Hop);
                Mvx.Resolve<HomePageViewModel>().HOPS.RemoveAt(index);
                Mvx.Resolve<HomePageViewModel>().HOPS.Save("Hops.xml", "HOPS");
                var ttt = Mvx.Resolve<HomePageViewModel>().HOPS;
                Mvx.Resolve<HomePageViewModel>().HOPS = null;
                Mvx.Resolve<HomePageViewModel>().HOPS = ttt;
                Mvx.Resolve<INavigationService>().GoBack();
            };
            appBar.Buttons.Add(btndelete);
            this.ApplicationBar = appBar;
        }
        #endregion

        public HopDetailPage()
        {
            InitializeComponent();
            BuildApplicationBar();
        }


        private void UIElement_OnLostFocus(object sender, RoutedEventArgs e)
        {
            TextBox textBox = (TextBox)sender;
            if(!textBox.Text.Contains("%"))
            textBox.Text = string.Format("{0} %", textBox.Text);
        }
    }
}